package com.example.idachuappone.index.dialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
import antistatic.spinnerwheel.OnWheelChangedListener;
import antistatic.spinnerwheel.OnWheelScrollListener;
import antistatic.spinnerwheel.WheelView;
import antistatic.spinnerwheel.adapters.AbstractWheelTextAdapter;

import com.example.idachuappone.R;
import com.example.idachuappone.index.entity.DateTime;
import com.example.idachuappone.utils.Utils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class DialogDateHours {
	private Context context;
	private Dialog dialogDateHours;
	private int hour;
	private int[] hourindex;
	private List<String> list3;
	private List<DateTime> lists;
	private MyDateTimeWheelViewAdapter myDateTimeWheelViewAdapter;
	private MyWheelViewAdapter myWheelViewAdapter2;
	private MyWheelViewAdapter myWheelViewAdapter3;
	private boolean scrolling;
	private WheelView wheelView1;
	private WheelView wheelView2;
	private WheelView wheelView3;
	private int yearindex;

	class AnonymousClass_1 implements OnClickListener {
		private final/* synthetic */CallBack val$callBack;

		AnonymousClass_1(CallBack callBack) {
			this.val$callBack = callBack;
		}

		@Override
		public void onClick(View v) {
			this.val$callBack.btnOk(DialogDateHours.this.lists.get(DialogDateHours.this.wheelView1.getCurrentItem()),
					DialogDateHours.this.wheelView2.getCurrentItem(),
					DialogDateHours.this.list3.get(DialogDateHours.this.wheelView3.getCurrentItem()));
		}
	}

	public static interface CallBack {
		void btnOk(DateTime dateTime, int i, String str);
	}

	private class MyDateTimeWheelViewAdapter extends AbstractWheelTextAdapter {
		private List<DateTime> dateTimes;

		protected MyDateTimeWheelViewAdapter(Context context, List<DateTime> list) {
			super(context, 2130903109, 0);
			setTextTypeface(Typeface.DEFAULT);
			setItemTextResource(R.id.date_ymd);
			this.dateTimes = list;
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			return super.getItem(index, cachedView, parent);
		}

		@Override
		public int getItemsCount() {
			return this.dateTimes == null ? 0 : this.dateTimes.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			if (this.dateTimes == null) {
				return null;
			}
			if (this.dateTimes.get(index).getDay() != null && this.dateTimes.get(index).getDay().length() > 0) {
				return this.dateTimes.get(index).getDay();
			}
			return this.dateTimes == null ? null : this.dateTimes.get(index).getYear();
		}
	}

	private class MyWheelViewAdapter extends AbstractWheelTextAdapter {
		private List<String> hourList;

		protected MyWheelViewAdapter(Context context, List<String> list) {
			super(context, 2130903109, 0);
			setTextTypeface(Typeface.DEFAULT);
			setItemTextResource(R.id.date_ymd);
			this.hourList = list;
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			return super.getItem(index, cachedView, parent);
		}

		@Override
		public int getItemsCount() {
			return this.hourList == null ? 0 : this.hourList.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			return (this.hourList == null || this.hourList == null) ? null : (String) this.hourList.get(index);
		}
	}

	public DialogDateHours(Context context) {
		this.context = context;
	}

	public Dialog showDateHours(Activity activity, CallBack callBack) {
		initDateHours();
		View view = LayoutInflater.from(this.context).inflate(R.layout.dialog_date_time_main, null);
		((TextView) view.findViewById(R.id.btn_date_hour_ok)).setOnClickListener(new AnonymousClass_1(callBack));
		this.wheelView1 = (WheelView) view.findViewById(R.id.wheelView1);
		this.wheelView2 = (WheelView) view.findViewById(R.id.wheelView2);
		this.myWheelViewAdapter2 = new MyWheelViewAdapter(this.context, this.lists.get(0).getList());
		this.wheelView2.setViewAdapter(this.myWheelViewAdapter2);
		this.wheelView2.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!DialogDateHours.this.scrolling) {
					DialogDateHours.this.hourindex[DialogDateHours.this.yearindex] = newValue;
				}
			}
		});
		this.myDateTimeWheelViewAdapter = new MyDateTimeWheelViewAdapter(this.context, this.lists);
		this.wheelView1.setViewAdapter(this.myDateTimeWheelViewAdapter);
		this.wheelView1.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (!DialogDateHours.this.scrolling) {
					DialogDateHours.this.updateCities(DialogDateHours.this.wheelView2, DialogDateHours.this.lists,
							newValue);
				}
			}
		});
		this.wheelView1.addScrollingListener(new OnWheelScrollListener() {
			@Override
			public void onScrollingStarted(WheelView wheel) {
				DialogDateHours.this.scrolling = true;
			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				DialogDateHours.this.scrolling = false;
				DialogDateHours.this.updateCities(DialogDateHours.this.wheelView2, DialogDateHours.this.lists,
						DialogDateHours.this.wheelView1.getCurrentItem());
			}
		});
		this.wheelView3 = (WheelView) view.findViewById(R.id.wheelView3);
		this.myWheelViewAdapter3 = new MyWheelViewAdapter(this.context, this.list3);
		this.wheelView3.setViewAdapter(this.myWheelViewAdapter3);
		this.wheelView1.setVisibleItems(BitmapCacheManagementTask.MESSAGE_CLOSE);
		this.wheelView2.setVisibleItems(BitmapCacheManagementTask.MESSAGE_CLOSE);
		this.wheelView3.setVisibleItems(BitmapCacheManagementTask.MESSAGE_CLOSE);
		view.setMinimumWidth((int) ((Utils.getScreenWidth(activity)) * 0.9d));
		Dialog dialog = new Dialog(this.context, 2131361801);
		dialog.setContentView(view);
		dialog.setCanceledOnTouchOutside(false);
		Window dialogWindow = dialog.getWindow();
		LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(17);
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);
		return dialog;
	}

	private void updateCities(WheelView city, List<DateTime> list, int index) {
		this.yearindex = index;
		city.setViewAdapter(new MyWheelViewAdapter(this.context, list.get(index).getList()));
		city.setCurrentItem(this.hourindex[index], true);
	}

	private void initDateHours() {
		int i;
		this.lists = new ArrayList();
		Calendar calendar = Calendar.getInstance();
		this.hour = calendar.get(12);
		this.hour += 5;
		boolean istoday = true;
		for (i = 0; i < 7; i++) {
			List<String> hours = new ArrayList();
			int j;
			if (this.hour > 21) {
				istoday = false;
				calendar.add(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY, 1);
				for (j = 11; j <= 21; j++) {
					hours.add(new StringBuilder(String.valueOf(j)).toString());
				}
			} else if (i == 0) {
				for (j = this.hour; j <= 21; j++) {
					hours.add(new StringBuilder(String.valueOf(j)).toString());
				}
			} else {
				for (j = 11; j <= 21; j++) {
					hours.add(new StringBuilder(String.valueOf(j)).toString());
				}
			}
			String day = "";
			if (istoday) {
				if (i == 0) {
					day = "\u4eca\u5929";
				} else if (i == 1) {
					day = "\u660e\u5929";
				} else if (i == 2) {
					day = "\u540e\u5929";
				}
			} else if (i == 0) {
				day = "\u660e\u5929";
			} else if (i == 1) {
				day = "\u540e\u5929";
			}
			String year = new StringBuilder(String.valueOf(calendar.get(BitmapCacheManagementTask.MESSAGE_FLUSH) + 1))
					.append("\u6708").append(calendar.get(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY))
					.append("\u65e5").toString();
			Log.e("aaaaaa", year);
			this.lists.add(new DateTime(day, calendar.get(1), year, hours));
			if (this.hour < 22) {
				calendar.add(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY, 1);
			}
		}
		this.list3 = new ArrayList();
		this.list3.add("00");
		this.list3.add("30");
		this.hourindex = new int[7];
		for (i = 0; i < this.hourindex.length; i++) {
			this.hourindex[i] = 0;
		}
	}
}
